/**
 * 
 */
package com.swim.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.shop.entity.Product;
import com.swim.shop.service.ProductService;

/**
 * 产品Controller
 * @author ljw
 * @version 2023-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/product")
public class ProductController extends BaseController {

	@Autowired
	private ProductService productService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Product get(String id, boolean isNewRecord) {
		return productService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("shop:product:view")
	@RequestMapping(value = {"list", ""})
	public String list(Product product, Model model) {
		model.addAttribute("product", product);
		return "swim/shop/productList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Product> listData(Product product, HttpServletRequest request, HttpServletResponse response) {
		Page<Product> page = productService.findPage(new Page<Product>(request, response), product); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("shop:product:view")
	@RequestMapping(value = "form")
	public String form(Product product, Model model) {
		model.addAttribute("product", product);
		return "swim/shop/productForm";
	}

	/**
	 * 保存产品
	 */
	@RequiresPermissions("shop:product:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Product product) {
		productService.save(product);
		return renderResult(Global.TRUE, text("保存产品成功！"));
	}
	
	/**
	 * 停用产品
	 */
	@RequiresPermissions("shop:product:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Product product) {
		product.setStatus(Product.STATUS_DISABLE);
		productService.updateStatus(product);
		return renderResult(Global.TRUE, text("停用产品成功"));
	}
	
	/**
	 * 启用产品
	 */
	@RequiresPermissions("shop:product:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Product product) {
		product.setStatus(Product.STATUS_NORMAL);
		productService.updateStatus(product);
		return renderResult(Global.TRUE, text("启用产品成功"));
	}
	
	/**
	 * 删除产品
	 */
	@RequiresPermissions("shop:product:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Product product) {
		productService.delete(product);
		return renderResult(Global.TRUE, text("删除产品成功！"));
	}
	
	/**
	 * 获取详情数据
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String productId) {
		return renderResult(Global.TRUE, text("获取详情数据成功！"), productService.get(productId));
	}
	
}